﻿Public Class MyDataView

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        'カスタム描画コードをここに追加します。
    End Sub

    '设置选择的位置
    Public iPos As Integer = 0

    Friend WithEvents Column1 As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents Column2 As System.Windows.Forms.DataGridViewTextBoxColumn

    '初始化控件信息
    Public Sub SetInfo()


        Me.Column1 = New System.Windows.Forms.DataGridViewTextBoxColumn
        Me.Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn
        '
        'Column1
        '
        Me.Column1.HeaderText = "编号"
        Me.Column1.Name = "Column1"
        Me.Column1.ReadOnly = True
        Me.Column1.Visible = False
        Me.Column1.Width = 5
        '
        'Column2
        '
        Me.Column2.HeaderText = "标题"
        Me.Column2.Name = "Column2"
        Me.Column2.Width = Me.Width - 3
        Me.Column2.ReadOnly = True
        Me.Column2.SortMode = DataGridViewColumnSortMode.NotSortable

        Me.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft
        Me.DefaultCellStyle.BackColor = System.Drawing.SystemColors.Window
        Me.DefaultCellStyle.ForeColor = System.Drawing.SystemColors.ControlText
        Me.DefaultCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight
        Me.DefaultCellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText
        Me.DefaultCellStyle.WrapMode = DataGridViewTriState.True

        Me.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        Me.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        Me.AllowUserToAddRows = False
        Me.AllowUserToDeleteRows = False
        Me.AllowUserToOrderColumns = False

        Me.Name = "DataGridView1"
        Me.ReadOnly = True
        Me.RowHeadersVisible = False
        Me.RowTemplate.Height = 20

        Me.AllowUserToDeleteRows = False
        Me.MultiSelect = False
        Me.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize

        Me.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1, Me.Column2})

    End Sub

#Region "单击时触发事件"

    Private Sub MyDataView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        If Me.SelectedRows.Count > 0 Then
            iPos = Me.SelectedRows(0).Index
            RaiseEvent ItemClickRefreshData(Me.SelectedRows(0).Cells(0).Value)
        End If
    End Sub

    Private Sub MyDataView_RowStateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowStateChangedEventArgs) Handles MyBase.RowStateChanged
        'If Me.SelectedRows.Count > 0 Then
        '    If Me.SelectedRows(0).Cells.Count > 0 Then
        '        iPos = Me.SelectedRows(0).Index
        '        RaiseEvent ItemClickRefreshData(Me.SelectedRows(0).Cells(0).Value)
        '    End If
        'End If
    End Sub
#End Region

#Region "窗体大小发生改变，控件列自动跟谁改变"

    Private Sub MyDataView_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        'If DesignMode Then
        '    Return
        'End If
        'If Me.ColumnCount > 0 Then
        '    If Me.Width > 7 Then
        '        If Column2 IsNot Nothing Then
        '            Me.Column2.Width = Me.Width - 2
        '        End If
        '    End If
        'End If
    End Sub
#End Region

    '自定义事件
    Public Event ItemClickRefreshData(ByVal strId As String)

    Public Sub SetSelectPos()
        If iPos < 0 Then Exit Sub
        If iPos >= Me.Rows.Count Then Exit Sub
        Me.Rows(iPos).Selected = True
    End Sub

    Public Sub SetSelectPos(ByVal strpos As Integer)
        Try
            If Me.Rows.Count = 0 Then Exit Sub
            If strpos < 0 Then strpos = 0
            If strpos >= Me.Rows.Count - 1 Then strpos = Me.Rows.Count - 1
            Me.Rows(strpos).Selected = True
            Me.FirstDisplayedScrollingRowIndex = strpos
            'iPos = Me.SelectedRows(0).Index
            'RaiseEvent ItemClickRefreshData(Me.Rows(strpos).Cells(0).Value)
        Catch ex As Exception
        Finally
        End Try

    End Sub

    Private Sub MyDataView_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        Try
            Me.Column1.Dispose()
            Me.Column2.Dispose()
        Catch ex As Exception

        End Try
    End Sub
End Class
